import asyncio

from metagpt.roles import Role
import re
from metagpt.schema import Message
from metagpt.actions import Action
from metagpt.logs import logger
from src.tools.Translated import Translated


# 待确认是否使用这个
# 初步对java文件进行分析：是否为controller/mapper/。。。。或者是其他的

# 分析的action行为
class Analyse(Action):
    PROMPT_TEMPLATE: str = """
    你是一个编程学习助手，现在关于以下知识点需要你给予答复
    {code}
    """

    name: str = "Programming Learning Assistant"

    async def run(self, code: str):
        prompt = self.PROMPT_TEMPLATE.format(code=code)

        rsp = await self._aask(prompt)


        return rsp



class Analyst(Role):
    name: str = "Code Analysis Engineer"
    profile: str = "Analyst"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.set_actions([Analyse])

    async def _act(self) -> Message:
        logger.info(f"{self._setting}: to do {self.rc.todo}({self.rc.todo.name})")
        todo = self.rc.todo  # todo will be SimpleWriteCode()

        msg = self.get_memories(k=1)[0]  # find the most recent messages
        code_text = await todo.run(msg.content)
        msg = Message(content=code_text, role=self.profile, cause_by=type(todo))

        return msg


async def main(data):
    role = Analyst()
    logger.info(data)
    result = await role.run(data)
    return result


def chat_main(data):
    resdata = asyncio.run(main(data))
    return resdata


if __name__ == '__main__':
    resdata= chat_main("素数")

    print(f"aaaa {resdata}")
    # main()
